#! /bin/bash

CUR_DIR=$(cd $(dirname "$0"); pwd)
IMAGE_DIR=""
DEVICE=""

# images name
BL1_FWBL_IMG="itop4412-fwbl.bin"
BL1_SPL_IMG="itop4412-spl.bin"
BL2_UBOOT_IMG="u-boot.bin"
TZSW_IMG="itop4412-tzsw.bin"
ENV_IMG="itop4412-env.bin"

# itop4412 images position
BL1_FWBL_POS=1
BL1_SPL_POS=17
BL2_UBOOT_POS=49
TZSW_POS=1073
ENV_POS=1385

function usage()
{
	echo "Usage:"
	echo "    $(basename $0) [/dev/sdX] [image-dir]"
	echo "    /dev/sdX    device file for sd card"
	echo "    image-dir   image direction, optional"
}

function bl1_fwbl_fusing()
{
	echo "BL1_FWBL fusing ..."
	dd iflag=dsync oflag=dsync if=$IMAGE_DIR/$BL1_FWBL_IMG of=$DEVICE seek=$BL1_FWBL_POS
	echo "Finish fusing BL1_FWBL image."
}

function bl1_spl_fusing()
{
	echo "BL1_SPL fusing ..."
	dd iflag=dsync oflag=dsync if=$IMAGE_DIR/$BL1_SPL_IMG of=$DEVICE seek=$BL1_SPL_POS
	echo "Finish fusing BL1_SPL image."
}

function bl2_uboot_fusing()
{
	echo "BL2_UBOOT fusing ..."
	dd iflag=dsync oflag=dsync if=$IMAGE_DIR/$BL2_UBOOT_IMG of=$DEVICE seek=$BL2_UBOOT_POS
	echo "Finish fusing BL2_UBOOT image."
}

function tzsw_fusing()
{
	echo "TZSW fusing ..."
	dd iflag=dsync oflag=dsync if=$IMAGE_DIR/$TZSW_IMG of=$DEVICE seek=$TZSW_POS
	echo "Finish fusing TZSW image."
}

function env_fusing()
{
	echo "ENV fusing ..."
	dd iflag=dsync oflag=dsync if=$IMAGE_DIR/$ENV_IMG of=$DEVICE seek=$ENV_POS
	echo "Finish fusing ENV image."
}

function main()
{
	echo "--------------------------[Begin]--------------------------"
	echo "May take a few minutes, wait ..."
	bl1_fwbl_fusing
	bl1_spl_fusing
	bl2_uboot_fusing
#	tzsw_fusing
#	env_fusing
	echo "--------------------------[End]----------------------------"
}

while getopts :h opt
do
	case $opt in
		h)
			usage $0
			exit 0
			;;
		?)
			echo "unknown option"
			;;
	esac
done

if [ $# -le 0 ] || [ $# -ge 3 ]
then
	echo "arguments too few or many."
	usage $0
	exit 1
else
	DEVICE="$1"
	if [ $# -eq 2 ]
	then
		IMAGE_DIR="$2"
	else
		IMAGE_DIR="$CUR_DIR"
	fi
fi

if [ -b "$DEVICE" ]
then
    echo "$DEVICE is identified."
else
    echo "$DEVICE is NOT device file."
    dd if=/dev/zero of=$DEVICE bs=512 count=2048
    BL1_FWBL_POS=0
    BL1_SPL_POS=31
    BL2_UBOOT_POS=62
    TZSW_POS=1086
    ENV_POS=1398
fi

main

sync;sync;sync
